package com.wuyou.mid;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * minCapability
 *
 * @Author 无忧
 * @Date 2023/9/19 11:07
 */
public class MinCapability {

    public static void main(String[] args) {
        MinCapability minCapability = new MinCapability();
//        int[] nums = {2, 2};
//        int[] nums = {4, 22, 11, 14, 25};
        int[] nums = {24, 109, 117, 142, 98, 94, 91, 130, 73, 48, 107, 77};
        int k = 5;
        System.out.println(minCapability.minCapability(nums, k));
    }

    public int minCapability(int[] nums, int k) {
        int lower = Arrays.stream(nums).min().getAsInt();
        int upper = Arrays.stream(nums).max().getAsInt();
        while (lower <= upper) {
            int middle = (lower + upper) / 2;
            int count = 0;
            boolean visited = false;
            for (int x : nums) {
                if (x <= middle && !visited) {
                    count++;
                    visited = true;
                } else {
                    visited = false;
                }
            }
            if (count >= k) {
                upper = middle - 1;
            } else {
                lower = middle + 1;
            }
        }
        return lower;
    }
}
